package cn.lds.health.resolver;

import cn.lds.health.dto.ResultDTO;
import cn.lds.health.exception.SysException;
import org.springframework.web.servlet.HandlerExceptionResolver;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class SysExceptionResolve implements HandlerExceptionResolver {
    @Override
    public ModelAndView resolveException(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, Exception e) {
        //创建自定义类型的变量
        SysException sysException = null;
        
        if(e instanceof SysException){
            //如果发生的一场属于我们异常了类型，就复制我自定义异常类变量
            sysException = (SysException) e;
        }else {
            //控制台输出异常
            e.printStackTrace();
            //创建自定义异常对象，放入客户可以接受的错误信息
            sysException = new SysException("系统升级中。。。。");
        }
        
        
        ModelAndView mv = new ModelAndView();
        mv.addObject("error", ResultDTO.buildError(sysException));
        mv.setViewName("error");

        return mv;
    }
}